package com.zpl.gateway.filter;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zpl.core.Result;
import com.zpl.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

import static com.zpl.gateway.constrant.GlobalConstrant.TOKEN_PASSWORD;

/**
 * @author wangyuxin
 * @date 2020/9/21
 */
@Slf4j
@Component
public class AuthGatewayFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        log.info("网关过滤器");
//        ServerHttpRequest request = exchange.getRequest();
//        ServerHttpResponse response = exchange.getResponse();
//
//        if (request.getPath().toString().contains("login")) {
//            return chain.filter(exchange);
//        }
//        // 获取令牌
//        HttpHeaders headers = request.getHeaders();
//        List<String> tokens = headers.get("admin_token");
//        Result result = Result.fail("没有认证");
//        if (null != tokens && tokens.size() > 0) {
//            String token = tokens.get(0);
//            // TODO 有令牌
//            if (StringUtils.isNotEmpty(token)) {
//                String username = JwtUtils.getUsername(token);
////                String password = Md5Utils.getMD5String(GlobalConstrant.JWT_SECRET);
////                // 防篡改
////                if (JwtUtils.verify(token, username, password)) {
////                    return chain.filter(exchange);
////                }
//
//                if (JwtUtils.verify(token, username, TOKEN_PASSWORD)) {
//                    return chain.filter(exchange);
//                }
//                else {
//                    result = Result.fail("令牌不正确");
//                }
//            }
//        }
//        // 设置body
//        String warningStr = JSON.toJSONString(result);
//        // 响应数据
//        DataBuffer bodyDataBuffer = response.bufferFactory().wrap(warningStr.getBytes());
//        return response.writeWith(Mono.just(bodyDataBuffer));
        return chain.filter(exchange);
    }

}
